import unittest
import os
import sys
import os.path

sys.path.append(os.path.realpath(os.path.pardir))
from configure_web_logs import *

class WebStatsConfigureTest(unittest.TestCase):
    
    def setUp(self):
        self.web_stats_test_obj = WebStatsConfigure("test_configuration.db3")
        
    def test_init(self):
        self.assertTrue(os.path.isfile(self.web_stats_test_obj.db_file_full_path))
        
    def test_add(self):
        web_stats_db_full_file_path = os.path.realpath(os.path.curdir + os.path.sep + "test_web_stats.db3")
        web_log_full_file_path = os.path.realpath(os.path.curdir) + os.path.sep + "creative.log"
        self.web_stats_test_obj.add(web_log_full_file_path,"sqlite:///%s" % web_stats_db_full_file_path, "|",0,3,2,6,4,5)
        session = self.web_stats_test_obj.Session()
        added_config = session.query(Configuration).filter_by(log_file_path = web_log_full_file_path).all()
        self.assertEquals(len(added_config),1)
        
    def test_get_hash(self):
        web_stats_db_full_file_path = os.path.realpath(os.path.curdir + os.path.sep + "test_web_stats.db3")
        web_log_full_file_path = os.path.realpath(os.path.curdir) + os.path.sep + "creative.log"
        self.web_stats_test_obj.add(web_log_full_file_path,"sqlite:///%s" % web_stats_db_full_file_path, "|",0,3,2,6,4,5)
        current_configuration = self.web_stats_test_obj.get_hash(1)
        
        self.assertTrue(type(current_configuration),type({}))
        self.assertEquals(current_configuration["delimiter"],"|")
        self.assertTrue(type(current_configuration["positions_in_log"]),type({}))
        self.assertEquals(current_configuration["positions_in_log"]["request_url"],2)
        
    def test_get_all(self):
        web_stats_db_full_file_path = os.path.realpath(os.path.curdir + os.path.sep + "test_web_stats.db3")
        web_log_full_file_path = os.path.realpath(os.path.curdir) + os.path.sep + "creative.log"
        self.web_stats_test_obj.add(web_log_full_file_path,"sqlite:///%s" % web_stats_db_full_file_path, "|",0,3,2,6,4,5)
        
        web_stats_db_full_file_path = os.path.realpath(os.path.curdir + os.path.sep + "test_web_stats.db3")
        web_log_full_file_path = os.path.realpath(os.path.curdir) + os.path.sep + "creative.log"
        self.web_stats_test_obj.add(web_log_full_file_path,"sqlite:///%s" % web_stats_db_full_file_path, "|",1,1,1,1,1,1)
        
        all_ids = self.web_stats_test_obj.get_all()
        self.assertEquals(type([]),type(all_ids))
        
    def test_set(self):
        web_stats_db_full_file_path = os.path.realpath(os.path.curdir + os.path.sep + "test_web_stats.db3")
        web_log_full_file_path = os.path.realpath(os.path.curdir) + os.path.sep + "creative.log"
        self.web_stats_test_obj.add(web_log_full_file_path,"sqlite:///%s" % web_stats_db_full_file_path, "|",0,3,2,6,4,5)
        configurations = self.web_stats_test_obj.get_all()
        c0 = configurations[0]
        
        self.web_stats_test_obj.set(c0.id, {"delimiter" : " "})
        configurations = self.web_stats_test_obj.get_all()
        c0 = configurations[0]
        
        self.assertEquals(c0.delimiter," ")
        
    def test_delete(self):
        web_stats_db_full_file_path = os.path.realpath(os.path.curdir + os.path.sep + "test_web_stats.db3")
        web_log_full_file_path = os.path.realpath(os.path.curdir) + os.path.sep + "creative.log"
        self.web_stats_test_obj.add(web_log_full_file_path,"sqlite:///%s" % web_stats_db_full_file_path, "|",0,3,2,6,4,5)
        configurations = self.web_stats_test_obj.get_all()
        c0 = configurations[0]
        self.web_stats_test_obj.delete(c0.id)
        self.assertEquals(len(self.web_stats_test_obj.get_all()),0)
        
    def tearDown(self):
        self.web_stats_test_obj.clean()

if __name__ == "__main__":
    unittest.main()